/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.UserData;
import data.IdentitasData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.UserDB;
import database.IdentitasDB;

/**
 *
 * @author Muhammad Dony
 */
public class IdentitasUserServlet extends HttpServlet {
    UserData listUser;
    IdentitasData listIdentitas;
    UserDB userdb;
    IdentitasDB identitasdb;
    HttpSession session;
    private String action;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void halamanIdentitas(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        UserData user = (UserData) session.getAttribute("userMasuk");
//        List listidentitas = new IdentitasDB().getIdentitasById(user.getID());
//        String ID  = request.getParameter("ID");
        request.setAttribute("title", "Identitas Diri");
        request.setAttribute("buttonName", "Simpan");
        List listidentitas = new IdentitasDB().getIdentitasById(user.getID());
        IdentitasData dataIdentitas = (IdentitasData) listidentitas.get(0);
        request.setAttribute("dataIdentitas", dataIdentitas);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/identitas_user.jsp");
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        session = request.getSession();
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        if (udata != null) {
            try {
                        halamanIdentitas(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(IdentitasUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(IdentitasUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
        }else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
                rd.forward(request, response);
        }

    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    // get session
        session = request.getSession();
        // mengambil object sesiUser dari session
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        action = request.getParameter("action");
        if (action.equals("Simpan")) {
                    try {
                        updateIdentitas(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(IdentitasUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(IdentitasUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    
//    
//    public void halamanEditIdentitas(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException, SQLException, ClassNotFoundException {
//        request.setAttribute("title", "Edit Identitas");
//        String ID  = request.getParameter("ID");
//        List listIdentitas = new IdentitasDB().getIdentitasById(ID);
//        IdentitasData dataIdentitas = (IdentitasData) listIdentitas.get(0);
//        request.setAttribute("dataIdentitas", dataIdentitas);
//                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/identitas_user.jsp");
//        rd.forward(request, response);
//    }

    public void updateIdentitas(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listIdentitas
        listIdentitas = new IdentitasData();
        // set listIdentitas
            listIdentitas.setID(request.getParameter("ID"));
            listIdentitas.setNama1(request.getParameter("nama1"));
            listIdentitas.setNama2(request.getParameter("nama2"));
            listIdentitas.setTempat(request.getParameter("tempat"));
            listIdentitas.setTanggal(request.getParameter("tanggal")); 
            listIdentitas.setBulan(request.getParameter("bulan"));
            listIdentitas.setTahun(request.getParameter("tahun"));
            listIdentitas.setAlamat(request.getParameter("alamat"));
            listIdentitas.setHp(request.getParameter("hp"));
            listIdentitas.setEmail(request.getParameter("email"));
            listIdentitas.setJurusan(request.getParameter("jurusan"));
            listIdentitas.setFakultas(request.getParameter("fakultas"));
            listIdentitas.setUniversitas(request.getParameter("universitas"));
            listIdentitas.setAngkatan(request.getParameter("angkatan"));
            listIdentitas.setTinggi(request.getParameter("tinggi"));
            listIdentitas.setBerat(request.getParameter("berat"));
            listIdentitas.setDarah(request.getParameter("darah"));
            listIdentitas.setMotto(request.getParameter("motto"));
        // memasukkannya dalam database
        identitasdb = new IdentitasDB();
        identitasdb.updateIdentitas(listIdentitas);
        // redirect ke halaman pelaporan
        halamanIdentitas(request, response);
    }
}
